package com.bardpub.weblite.utils 
{
	import flash.external.ExternalInterface;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName 
	 * @author LittleBard
	 * @version 1.0
	 * @date 
	 * @descrition 
	 * @others
	 * @history
	 */
	
	public class Broswer
	{
		/**
		 * 创建com.bardpub.weblite.utils.Broswer 类的新实例
		 */
		public function Broswer() 
		{
			
		}
		
		/**
		 * 在浏览器中弹出警告窗口
		 * @param	szMsg 警告消息
		 */
		public static function alert(szMsg:String):void 
		{
			ExternalInterface.call("alert", szMsg);
		}
		
		/**
		 * 设置页面标题
		 * @param	caption 页面标题
		 */
		public static function setCaption(caption:String):void {
			ExternalInterface.call("function(caption) {document.title=caption}", caption);
		}
		
		/**
		 * 设置锚点
		 * @param	szHash 锚点
		 */
		public static function setHash(szHash:String):void {
			ExternalInterface.call("function(hash) {location.hash=hash}", szHash);
		}
		
		/**
		 * 获取锚点
		 */
		public static function GetHash():String {
			var hash:String = ExternalInterface.call("function(){return location.hash;}");
			if (hash != null && hash.length > 1) {
				hash = hash.substr(1);
			}else {
				hash = null;
			}
			return hash;
		}
		
		/**
		 * 在浏览器中打开新窗口，依赖于JavaScript window.open 函数
		 * @param	url 要打开窗口的URL
		 * @param	target 打开方式，_blank弹出新窗口, _self在本窗口跳转
		 * @param	name 打开窗口的名字
		 * @param	features 设定新窗口的功能
		 * @param	replace	是否创建新的历史记录
		 */
		public static function open(url:String, 
								    target:String = "_blank", 
									name:String = null, 
									features:String = null, 
									replace:Boolean = true):void 
		{
			var WINDOW_OPEN_FUNCTION:String = "window.open";
			var myURL:URLRequest = new URLRequest(url);
			if (target != "_blank") {
					navigateToURL(myURL, target);
					return;
			}
			var browserName:String = getBrowserName();
			if (getBrowserName() == "Firefox") {
				ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, target);
			} else if (browserName == "IE") {
				ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, target);
			} else if (browserName == "Safari") {
				navigateToURL(myURL, target);
			} else if (browserName == "Opera") {
				navigateToURL(myURL, target);
			} else {
				navigateToURL(myURL, target);
			}
		}
		
		/**
		 * 获取浏览器名
		 * @return 浏览器名
		 */
		public static function getBrowserName():String {
			var browser:String;
			var browserAgent:String = ExternalInterface.call("function(){return navigator.userAgent;}");
			if (browserAgent != null && browserAgent.indexOf("Firefox") >= 0) {
				browser = "Firefox";
			} else if (browserAgent != null && browserAgent.indexOf("Safari") >= 0) {
				browser = "Safari";
			} else if (browserAgent != null && browserAgent.indexOf("MSIE") >= 0) {
				browser = "IE";
			} else if (browserAgent != null && browserAgent.indexOf("Opera") >= 0) {
				browser = "Opera";
			} else {
				browser = "Undefined";
			}
			return browser;
		}
	}

}